package com.mall.product.controller;

import com.github.pagehelper.PageInfo;
import com.mall.entity.TbGoods;
import com.mall.product.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 产品控制器
 * created with IntelliJ IDEA
 *
 * @author ASUS
 * @Auther: W
 * @Date: 2021/07/16/10:50
 * Other:
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    /**
     * 添加商品
     *
     * @param goods 货物
     * @return {@link Long}
     */
    @PostMapping("/addGoods")
    public Long addGoods(@RequestBody TbGoods goods) {
        try {
            return goodsService.addGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public List<TbGoods> findAll() {
        return goodsService.findAll();
    }


    /**
     *
     * 查询首页商品列表
     *
     * @param page 页面
     * @param size 大小
     * @return {@link PageInfo}
     */
    @GetMapping("/findList")
    public PageInfo findList(
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "size",defaultValue = "3") int size
    ){
        return goodsService.findList(page,size);
    }


    /**
     * 根据分类id查询商品信息
     *
     * @param typeId id类型
     * @return {@link List<TbGoods>}
     */
    @GetMapping("/findListByTypeId")
    public List<TbGoods> findListByTypeId(@RequestParam("typeId") Integer typeId){
        return goodsService.findListByTypeId(typeId);
    }

    /**
     * 根据id查询商品信息
     * @param id
     * @return TbGoods
     */
    @GetMapping("/findById")
    public TbGoods findById(@RequestParam("id") Long id){
        return goodsService.findById(id);
    }

}
